<template>
  <div class="page">
    <v-pageTitle title="日志管理"></v-pageTitle>
    <imp-panel>
      <!--工具条-->
      <v-pageToolbar align="left">
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item label="查询条件">
            <el-date-picker
              v-model="formSearch.value"
              type="daterange"
              size="small"
              align="right"
              placeholder="选择日期范围"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="onSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </v-pageToolbar>
      
      <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="right">
        <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%" :default-sort="{prop: 'date', order: 'descending'}">
            <el-table-column type="index" label="序号" width="120" align="center">
            </el-table-column>
            <el-table-column prop="logName" label="日志名称" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="logType" label="日志类型" min-width="120" sortable>
              <template slot-scope="scope">
                <el-tag
                  :type="scope.row.logType | logTypeFilterType">{{scope.row.logType | logTypeFilter}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="status" label="是否成功" min-width="120" sortable>
              <template slot-scope="scope">
                <el-tag
                  :type="scope.row.status | statusFilterType">{{scope.row.status | statusFilter}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="120" sortable>
            </el-table-column>
        </el-table>
      </v-pageTable>
    </imp-panel>
  </div>
</template>
<script>
import panel from "@/components/common/panel.vue"
export default {
  components: {
    'imp-panel': panel
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      formSearch:{
        value: new Date()
      },
      tableData: {
          loading: true,
          head: [],
          body: []
      },
      paginationTotal: 100
    }
  },
  filters: {
    statusFilter(statue) {
      let sta = statue ? 1 : 0;
      let statusMap = ['失败','成功']
      return statusMap[sta]
    },
    statusFilterType(statue) {
      let sta = statue ? 1 : 0;
      let statusMapType = ['error','success']
      return statusMapType[sta]
    },
    logTypeFilter(statue) {
      let sta = statue ? 1 : 0;
      let statusMap = ['严重日志','正常日志']
      return statusMap[sta]
    },
    logTypeFilterType(statue) {
      let sta = statue ? 1 : 0;
      let statusMapType = ['error','success']
      return statusMapType[sta]
    }
  },
  methods: {
    onSearch(){
      console.log(this.formSearch);
    },
    getTableData(data) {
      // ajax 查询表格数据
      this.tableData.body = [
        {
            logName: '日志1',
            logType: 0,
            createTime: '2018-1-2',
            remark: '删除用户1',
            status: true
        }, {
            logName: '日志2',
            logType: 1,
            createTime: '2018-1-15',
            remark: '修改权限1',
            status: false
        }
      ]
      // 模拟加载
      setTimeout(() => {
          this.tableData.loading = false
      }, 1000)
    },
    sizeChange(val){
        console.log(val)
        this.getTableData()
    },
    pageChange(val){
        console.log(val)
        this.getTableData()
    }
  },
  mounted() {
      // 获取table数据
      this.getTableData();
  },
  beforeDestory(){
  }
}
</script>